package com.lanhai.longheng.dispatch;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 


/**
 * 小车指令缓存
 * @author zhengwei
 *
 */
public class AgvCmdMap {
	 static Map<Integer,String> dispatchMap ;  //指令处理锁 agv:1
	 static Map<Integer,String> transingMap ; //传输锁  agv:1 
	 static Object dislocker=new Object();
	 static Object translocker=new Object(); 
	 	static {
	 		dispatchMap=Collections.synchronizedMap(new HashMap<Integer,String>());
	 		transingMap=Collections.synchronizedMap( new HashMap<Integer,String>()); 
	 	}

		 private static final Logger logger =  LogManager.getLogger("agvlog");
	  
	 	 static Map<Integer,Long> timeMap =Collections.synchronizedMap( new HashMap<Integer,Long>()); 
	 	
	 	/**
	 	 * 设置传输状态
	 	 * @param agvid
	 	 * @param flag
	 	 */
	 	public static   void  setTransing(int agvid,boolean flag) {
	 		synchronized (translocker) { 
		 		if(flag) { 
		 			transingMap.put(agvid, "1");
		 		}else { 
		 			transingMap.put(agvid, "");
		 		}
	 		}
	 	}
	 	
	 	
		/**
		 * 是否在传输中
		 * @param agvid
		 * @return
		 */
		public static   boolean isTransing(int agvid ) {
	 		 String m=transingMap.get(agvid);
	 		 if(m==null||m.equals("1")==false) {
	 			 return false;
	 		 }
	 		 return true;
	 	}
		
		/**
	 	 * 设置线程处理状态
	 	 * @param agvid
	 	 * @param flag
	 	 */
	 	public static   void  setDispatch(int agvid,boolean flag) {
	 		synchronized (dislocker) { 
		 		if(flag) {
		 			timeMap.put(agvid, System.currentTimeMillis());
		 			dispatchMap.put(agvid, "1");
		 		}else {
		 			dispatchMap.put(agvid, ""); 
		 			Long k=timeMap.get(agvid);
		 			if(k!=null) {
		 				Long t=System.currentTimeMillis()-k; 
			 			logger.info(String.format("agv:%d执行时间:%d ms", agvid,t));
		 			}
		 			
		 			 
		 		}
	 		}
	 	}
	 	
	 	
		/**
		 * 是否在其它线程处理中
		 * @param agvid
		 * @return
		 */
		public static   boolean isDispatch(int agvid ) {
	 		 String m=dispatchMap.get(agvid);
	 		 if(m==null||m.equals("1")==false) {
	 			 return false;
	 		 }
	 		 return true;
	 	}


 
 }

